package com.wanxi.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.education.mapper.UserMapper;
import com.wanxi.education.model.pojo.User;
import com.wanxi.education.model.vo.agency.AgencyVo;
import com.wanxi.education.model.vo.user.CUserReturnVo;
import com.wanxi.education.model.vo.user.CUserVo;
import com.wanxi.education.model.vo.user.FindByIdCUserVo;
import com.wanxi.education.model.vo.user.UpdateCUserVo;
import com.wanxi.education.service.AgencyService;
import com.wanxi.education.service.CUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: YanZhengYi
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Service
public class CUserServiceImpl implements CUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AgencyService agencyService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public PageInfo<CUserReturnVo> cUserFindPage(CUserVo cUserVo) {
        PageHelper.startPage(cUserVo.getPageNum(), cUserVo.getPageSize());
        List<CUserReturnVo> cUserReturnVoList = userMapper.cUserFindPage(cUserVo);
        return new PageInfo<>(cUserReturnVoList);
    }

    @Override
    public Integer delete(Long id) {
        Integer result = userMapper.delete(id);
        if (result != 1) {
            return null;
        }
        return result;
    }

    @Override
    @Transactional
    public String cUserInsert(UpdateCUserVo updateCUserVo) {
        String phone = updateCUserVo.getPhone();
        User user = userMapper.findByPhoneAndStatusAndId(phone);
        if (user!=null){
            userMapper.cUserUpdate(updateCUserVo);
        }
        try {
            updateCUserVo.setPassword(passwordEncoder.encode(updateCUserVo.getPassword()));
            Integer result = userMapper.cUserInsert(updateCUserVo);
            List<Integer> id = new ArrayList<>();
            id.add(4);
            userMapper.insertRole(id, updateCUserVo.getId());
            if (result != 1) {
                return "增加失败";
            }
            return "success";
        }catch (Exception e){
            return "电话重复";
        }

    }

    @Override
    public FindByIdCUserVo findById(Long id) {
        User user = userMapper.findById(id);
        //通过机构id找到机构的名称,再赋值给new的findByIdCUserVo的机构名称属性
        if (user == null) return null;
        FindByIdCUserVo findByIdCUserVo = new FindByIdCUserVo();
        AgencyVo byId = agencyService.findById(user.getAgencyId());
        BeanUtils.copyProperties(user, findByIdCUserVo);
        String name = byId.getName();
        findByIdCUserVo.setAgencyName(name);
        return findByIdCUserVo;
    }

    @Override
    public Integer cUserUpdate(UpdateCUserVo updateCUserVo) {
        String password = updateCUserVo.getPassword();
        if (password==null){
            User byId = userMapper.findById(updateCUserVo.id);
            updateCUserVo.setPassword(byId.getPassword());
        }
        try {
            updateCUserVo.setPassword(passwordEncoder.encode(updateCUserVo.getPassword()));
            Integer result = userMapper.cUserUpdate(updateCUserVo);
            if (result != 1) {
                return null;
            }
            return result;

        } catch (Exception e) {
            return null;
        }
    }
}
